<?php

namespace loggedin\view;

class GameView {
	private static $startgame = "startgame";
	private static $questionReplied = "questionReplied";
	private static $answerdQuestionIDCookie = "answerdQuestionIDCookie";
	private static $questionAnswer = "answer";
	private static $playerPoints = "playerPoints";
	private static $message = "message";

	/**
	 * @return boolean 
	 */
	public function checkIfFirstTimePlaying() {
		return isset($_COOKIE[self::$playerPoints]);
	}
	/**
	 * @return boolean 
	 */
	public function checkIfFirstQuestion() {
		return isset($_COOKIE[self::$answerdQuestionIDCookie]);
	}
	/**
	 * @return boolean 
	 */
	public function getStartGameInfo() {
		return isset($_GET[self::$startgame]);
	}
	/**
	 * @return boolean 
	 */
	public function getQuestionRepliedInfo() {
		return isset($_GET[self::$questionReplied]);
	}
	/**
	 * @return string 
	 */
	public function getAnswerdQuestionValue() {
		return $_POST[self::$questionAnswer];
	}
	/**
	 * @return string 
	 */
	public function getQuestionAnswerdID() {
		return $_COOKIE[self::$answerdQuestionIDCookie];
	}

	public function removeQuestionAnswerID() {
		setcookie(self::$answerdQuestionIDCookie, 0, time()-3600);
	}

	public function setSuccessMessage() {
		setcookie(self::$message, "Du svarade rätt.");
	}

	public function setFailMessage($rightAnswer) {
		setcookie(self::$message, "Du svarade fel. Rätt svar är: $rightAnswer");
	}

	public function setQuestionAnswerdID($questionID) {
		setcookie(self::$answerdQuestionIDCookie, $questionID, time()+3600);
	}

	public function setPlayerPoints($newPoints) {
		setcookie(self::$playerPoints, $newPoints, time()+(10*365*24*60*60));
	}

	public function updatePlayerPoints($newPoints) {
		$tempPoint = (int)$_COOKIE[self::$playerPoints]+(int)$newPoints;
		setcookie(self::$playerPoints, $tempPoint, time()+(10*365*24*60*60));
	}

	public function removeMessage() {
		setcookie(self::$message, "", time()-3600);
	}

	/**
	 * @param  loggedin\model\Question $question
	 * @return string HTML
	 */
	public function questionToGameHTML(\loggedin\model\Question $question) {
		$questionID = $question->getQuestionID();
		$questionString = $question->getQuestion();
		$questionAnswers = array($question->getRightAnswer(),
								 $question->getWrongAnswer1(),
								 $question->getWrongAnswer2(),
								 $question->getWrongAnswer3());
		shuffle($questionAnswers);
		$answerPos1 = $questionAnswers[0];
		$answerPos2 = $questionAnswers[1];
		$answerPos3 = $questionAnswers[2];
		$answerPos4 = $questionAnswers[3];
		$message = "";
		if(isset($_COOKIE[self::$message])) {
			$message = $_COOKIE[self::$message];
		}
		return "<!DOCTYPE HTML SYSTEM>
		<html>
			<head>
				<title>Quizzer inloggad</title>
				<meta charset='UTF-8'>
			</head>
			<body>
				<div class='wrapper'>
					<div class='leftBox'>
						<h1>Quizzer</h1>
						<p><a href='?'>Tillbaka</a></p>
						<p>" . $_COOKIE[self::$playerPoints] . "</p>
					</div>

					<div class='mainContent'>
						<br>
						<p>" . $message . "</p>
						<form method='post' action='?" . self::$questionReplied . "'>
							<h4>$questionString</h4>
							<input type='radio' name='answer' value='$answerPos1'> <strong>$answerPos1</strong><br />
							<input type='radio' name='answer' value='$answerPos2'> <strong>$answerPos2</strong><br />
							<input type='radio' name='answer' value='$answerPos3'> <strong>$answerPos3</strong><br />
							<input type='radio' name='answer' value='$answerPos4'> <strong>$answerPos4</strong><br />
							<input type='submit' value='Svara'>
						</form>
					</div>
				</div>
			</body>
		</html>";
	}
}